<?php

class User extends Core {
    
    function __construct() {
        
        parent::__construct();
        
        $this->load->language('user');
        $this->load->helper('language');
        $this->load->model('moduser');
        
    }
    
    
    public function login_form() {
        
        $this->load->helper('form');
        $this->is_history = false;
        $data = array();
        
        if ($this->input->post('login_form')) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', 'form_login', 'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('password', 'form_password', 'trim|required|xss_clean');
        
            if ($this->form_validation->run()) {
                
                if ($this->moduser->login(set_value('username'), set_value('password'))) {
                    
                    $data['notification'] = $this->lang->line('success_login');
                    
                    if ($this->load->settings('redirect_back')) {
                        
                        redirect_back();
                    }
                }
                else {
                    
                    $data['notification'] = $this->moduser->get_errors();
                }
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        parent::render($data, 'login_form');
    }
    
    
    public function register_form() {
        
        $this->load->helper('form');
        $this->is_history = false;
        $data = array();
        
        if ($this->input->post('register_form')) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', 'form_login', 'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('email', 'form_email', 'trim|required|valid_email|xss_clean');
            $this->form_validation->set_rules('password', 'form_password', 'trim|required|matches[password_confirm]|xss_clean');
            $this->form_validation->set_rules('password_confirm', 'form_password_confirm', 'trim|required|xss_clean');
        
            if ($this->form_validation->run()) {
                
                if ($this->moduser->add(set_value('username'), set_value('email'), set_value('password'))) {
                    
                    $data['notification'] = $this->lang->line('success_register');
                    
                    if ($this->load->settings('redirect_back')) {
                        
                        redirect_back();
                    }
                }
                else {
                    
                    $data['notification'] = $this->moduser->get_errors();
                }
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        parent::render($data, 'register_form');
    }
    
    
    public function logout() {
        
        $this->is_history = false;
        $this->moduser->logout();
        
        if ($this->load->settings('redirect_back')) {
                        
            redirect_back();
        }
        
        parent::render($this->lang->line('success_logout'));
    }
    
    
    public function user_list() {
        
        $data['users'] = $this->moduser->get_users()->result_array();
        $data['profile_url'] = $this->load->settings('profile_url');
        
        parent::render($data, 'list');
    }
    
    
    public function profile_byid($id = false) {
        
        if ($id === false) {
            
            $id = $this->usr->id;
        }
        
        $data['user'] = $this->moduser->get_users(array('user_id' => $id))->row_array();
        
        if ($this->load->settings('login_title')) {
            
            $this->template->set_title($data['user']['user_login']);
        }
        
        parent::render($data, 'profile');
    }
    
    //---Widgets---//
    
    public function widget_user_info($profile_url = '') {
                
        $data['user'] = $this->usr;
        $data['profile_url'] = $profile_url;
        
        parent::render($data, 'info');
    }
}